<?php

    require './tmp/cos-php-sdk-v5/vendor/autoload.php';

//    返回数据结构 code 为 1 成功   code 为 0 失败

    /**
     * 调用cos的类
     * Class cos
     */
    class cos{

        private $secretId = "AKIDV0FEyThfNoyqTosfCSR9bmgJfkY5LK2T"; //"云 API 密钥 SecretId";
        private $secretKey = "B6k7D2arra3FAoBH5cssbV8VbQxEFzSd"; //"云 API 密钥 SecretKey";
        private $region = "ap-beijing"; //设置一个默认的存储桶地域
        public $bucket = 'top237-1306391227'; //设置一个默认的存储桶 top237-1306391227
        //设置存储桶
        function setBucket($bt){
            $this->bucket = $bt;
        }
        //设置地域
        function setRegion($ri){
            $this->region = $ri;
        }
        //获取Key
        function getKey(){

            if($_POST['key']){
                return $_POST['key'];
            }else{

                $res = Array(
                    'code'=>0,
                    'msg'=>'请求请传入key值 文件名'
                );
                echo json_encode($res);
                die; //终止程序运行
            }
        }
        //错误时的统计处理函数
        function err(){
            $res = Array('code' => 0,'msg' =>'数据上传失败,请联系管理员或检查网络');
            echo json_encode($res);
        }
        //初始化一个cos对象
        function cosObj () {

            $cosClient = new Qcloud\Cos\Client(array(
                    'region' => $this->region,
                    'schema' => 'https', //协议头部，默认为http
                    'credentials'=> array(
                        'secretId'  => $this->secretId ,
                        'secretKey' => $this->secretKey)
                )
            );

            return $cosClient;
        }

        //$key 为对象键，对象键是对象在存储桶中的唯一标识
        //$srcPath 本地文件绝对路径
        function uploadFile(){

            $cosClient = $this->cosObj();
            $srcPath = $_FILES['file']['tmp_name'];


            ### 上传文件流
            try {

                $file = fopen($srcPath, "rb");
                if ($file) {

                    $result = $cosClient->putObject(array(
                        'Bucket' => $this->bucket,
                        'Key' => $this->getKey(),
                        'Body' => $file));

                    $res = Array('location'=>$result['Location'], 'code' => 1,'msg' =>'数据上传成功');

                     echo json_encode($res);
                }
                unlink($srcPath);
            } catch (\Exception $e) {
                err();
            }
        }

        //获取文件url
        function getFileUrl(){
            try {

                $bucket = $this->bucket; //存储桶，格式：BucketName-APPID
                $signedUrl = $this->cosObj()->getObjectUrl($bucket, $this->getKey(), '+10 minutes');
                // 请求成功
                $res = Array(
                    'code'=>1,
                    'url'=>$signedUrl,
                    'msg'=>'链接获取成功'
                );
                echo json_encode($res);
            } catch (\Exception $e) {
                err();
            }
        }
        //下载文件
        function downFile(){


            try {
                $bucket = $this->bucket; //存储桶，格式：BucketName-APPID
                $localPath = __DIR__."\\putTest\\".$this->getKey();//下载到本地指定路径
                $result = $this->cosObj()->getObject(array(
                    'Bucket' => $bucket,
                    'Key' => $this->getKey(),
                    'SaveAs' => $localPath));
                $res = Array('downUrl'=>$localPath,'code' => 1,'msg' =>'数据下载成功');
                echo json_encode($res);
            } catch (\Exception $e) {
                // 请求失败
                err();
            }
        }
        //删除文件
        function delFile(){


            try {
                $bucket = $this->bucket; //存储桶，格式：BucketName-APPID

                $result = $this->cosObj()->deleteObject(array(
                    'Bucket' => $bucket,
                    'Key' => $this->getKey(),
                ));
                // 请求成功
                $res = Array('code' => 1,'msg' =>'文件删除成功');
                echo json_encode($res);
            } catch (\Exception $e) {
                // 请求失败
                err();
            }
        }

    }


?>